using FSM.IBll; 
using FSM.Model; 
using FSM.Web.Models; 
using FSM.Web.Models.Fileter;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
 
namespace FSM.Web.Controllers 
{
    [Authorize]
    public class SysRoleController : BaseController 
    { 
        #region 模块 
 
        private readonly DataResultModel drm; 
 
        private readonly ISysRoleBll _bll;

        private readonly ISysMenuBll _sysMenuBll;

        private readonly IRelRoleMenuBll _relRoleMenuBll;
 
        #endregion 
 
        #region 构造函数 
 
        public SysRoleController(ISysRoleBll bll, ISysMenuBll sysMenuBll, IRelRoleMenuBll relRoleMenuBll) 
        { 
            _bll = bll;
            _sysMenuBll = sysMenuBll;
            _relRoleMenuBll = relRoleMenuBll;
            ModuleNo = "0104";
            drm = new DataResultModel(); 
        } 
 
        #endregion 
 
        #region 角色
 
        public IActionResult Index() 
        { 
            return View(); 
        } 
 
        [HttpPost]
        [ResultExceptionFilter] 
        [OperateAuthorize("0104", "Query")]
        public JsonResult Get(string id) 
        { 
            sys_role model = _bll.Get(id); 
            drm.success = true; 
            drm.data = model; 
            return drm.ToJson(); 
        } 
 
        /// <summary> 
        /// 查询 
        /// </summary> 
        /// <param name="role_no">角色编号</param> 
        /// <param name="role_name">角色名称</param> 
        [HttpPost] 
        [ResultExceptionFilter]
        [OperateAuthorize("0104", "Query")]
        public JsonResult List(string role_no, string role_name, int? page = 1, int? page_size = 30) 
        { 
            Tuple<List<sys_role>, int> result = _bll.List(role_no, role_name, page.Value, page_size.Value); 
            drm.success = true; 
            drm.data = result.Item1; 
            drm.total = result.Item2; 
            return new JsonResult(drm); 
        } 
 
        /// <summary> 
        /// 新增 
        /// </summary> 
        /// <param name="model">实体</param> 
        /// <returns></returns> 
        [HttpPost] 
        [ResultExceptionFilter]
        [OperateAuthorize("0104", "Add")]
        public JsonResult Insert(sys_role model) 
        { 
            model.creater = AuthencationHelper.GetLoginUserNo(User); 
            bool success = _bll.Insert(model); 
            drm.success = success; 
            drm.data = model; 
            return new JsonResult(drm); 
        } 
 
        /// <summary> 
        /// 更新 
        /// </summary> 
        /// <param name="model">实体</param> 
        /// <returns></returns> 
        [HttpPost] 
        [ResultExceptionFilter]
        [OperateAuthorize("0104", "Edit")]
        public JsonResult Update(sys_role model) 
        { 
            model.editer = AuthencationHelper.GetLoginUserNo(User); 
            bool success = _bll.Update(model); 
            drm.success = success; 
            drm.data = model; 
            return new JsonResult(drm); 
        } 
 
        /// <summary> 
        /// 删除 
        /// </summary> 
        /// <param name="role_no">键值</param> 
        /// <returns></returns> 
        [HttpPost] 
        [ResultExceptionFilter]
        [OperateAuthorize("0104", "Delete")]
        public JsonResult Delete(string role_nos) 
        { 
            bool success = _bll.Delete(role_nos.Split(',')); 
            drm.success = success; 
            return new JsonResult(drm); 
        }

        #endregion

        #region 菜单 

        /// <summary>
        /// 获得菜单树信息
        /// </summary>
        /// <param name="p_no">上级编号</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        public JsonResult Tree(string p_no)
        {
            List<sys_menu> treeList = _sysMenuBll.TreeContainActions(p_no);
            drm.success = true;
            drm.data = treeList;
            return drm.ToJson();
        }

        /// <summary>
        /// 获得角色拥有的菜单信息
        /// </summary>
        /// <param name="id">角色编号</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        public JsonResult MenusAndActions(string id)
        {
            List<string> result = _relRoleMenuBll.ListMenusAndActionKeysByRoleNo(id);
            drm.success = true;
            drm.data = result;
            return drm.ToJson();
        }

        /// <summary>
        /// 赋于角色对应的菜单信息
        /// </summary>
        /// <param name="id">角色编号</param>
        /// <param name="menu_nos">菜单编号</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize("0104", "Power")]
        public JsonResult GrantMenus(string id, string menu_nos)
        {
            string userNo = AuthencationHelper.GetLoginUserNo(User);
            drm.success = _relRoleMenuBll.BatchInsert(userNo, id, (menu_nos ?? "").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList());
            if (!drm.success)
            {
                throw new Exception("操作失败");
            }
            return drm.ToJson();
        }

        #endregion
    }
} 
